package com.pjb.springbootjjwt.store.utils;

import com.pjb.springbootjjwt.store.entity.Clause;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class StringUtil {

    public static String md5(String c) {
        return DigestUtils.md5Hex(c);
    }

    public static String uuid() {
        return UUID.randomUUID().toString();
    }

    public static String random(int bit) {
        double d = Math.random() * Math.pow(10, bit);
        return "" + (int) d;
    }

    /**
     * 返回空白字符
     *
     * @param len
     * @return
     */
    public static String space(int len) {
        byte[] tail = new byte[len];
        for (int i = 0; i < len; i++) {
            tail[i] = 32;
        }
        return new String(tail);
    }

    /**
     * 覆盖固定长度空白的字符串
     *
     * @param c
     * @param length
     * @return
     */
    public static String over(String c, int length) throws Exception {
        if (StringUtils.isEmpty(c)) return space(length);

        int d = length - c.length();
        if (d < 0) throw new Exception("字符串长度超过允许的空白长度！");

        return c + space(d);
    }

    public static String over(byte fill, int len) {
        byte[] tail = new byte[len];
        for (int i = 0; i < len; i++) {
            tail[i] = fill;
        }
        return new String(tail);
    }

    public static String over(byte fill, int len, int num) {
        len = len - ("" + num).length();
        byte[] tail = new byte[len];
        for (int i = 0; i < len; i++) {
            tail[i] = fill;
        }
        return new String(tail) + num;
    }

    public static int total(List<Integer> list) {
        int len = 0;
        for (Integer i : list) {
            len += i;
        }
        return len;
    }

    public static String sub(String sql, String pre) throws Exception {
        if (!sql.contains(pre)) return "";

        return sql.substring(sql.indexOf(pre)).replaceAll(pre, "").trim();
    }

    public static String sub(String sql, String from, String to) {
        if (!sql.contains(from)) return "";

        if (sql.contains(to)) {
            sql = sql.substring(sql.indexOf(from), sql.indexOf(to));
        } else {
            sql = sql.substring(sql.indexOf(from));
        }
        return sql.substring(sql.indexOf(from)).replaceAll(from, "").trim();
    }

    public static void main(String[] args) throws Exception {

    }

}
